package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.model.OrderExample;
import com.cskaoyan.model.User;
import com.cskaoyan.model.UserExample;
import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.WXUserOrderVO;
import com.cskaoyan.model.vo.WXUserVO;
import com.cskaoyan.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：王李轶群
 * @description：TODO
 * @date ：5/7/22 3:52 PM
 */
@Service
@Transactional
public class WxUserServiceImpl implements WxUserService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public BaseRespVO<WXUserVO> getUserIndex(String username) {
        short unpaid = 101;
        short unship = 201;
        short unrecv = 301;
        short uncomment = 401;
        Integer unpaidOrders = getOrderStatusCount(username, unpaid);
        Integer unrecvOrders = getOrderStatusCount(username, unrecv);
        Integer unshipOrders = getOrderStatusCount(username, unship);
        Integer uncommentOrders = getOrderStatusCount(username, uncomment);
        WXUserOrderVO wxUserOrderVO = new WXUserOrderVO();
        wxUserOrderVO.setUncomment(uncommentOrders);
        wxUserOrderVO.setUnpaid(unpaidOrders);
        wxUserOrderVO.setUnrecv(unrecvOrders);
        wxUserOrderVO.setUnship(unshipOrders);
        WXUserVO wxUserVO = new WXUserVO();
        wxUserVO.setOrder(wxUserOrderVO);
        BaseRespVO<WXUserVO> wxUserVOBaseRespVO = new BaseRespVO<>();
        wxUserVOBaseRespVO.setData(wxUserVO);
        wxUserVOBaseRespVO.setErrno(0);
        wxUserVOBaseRespVO.setErrmsg("成功");
        return wxUserVOBaseRespVO;
    }

    private Integer getOrderStatusCount(String username, short status) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria1 = userExample.createCriteria();
        criteria1.andUsernameEqualTo(username);
        criteria1.andDeletedEqualTo(false);
        List<User> users = userMapper.selectByExample(userExample);
        User user = users.get(0);
        Integer userId = user.getId();
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andOrderStatusEqualTo(status);
        criteria.andDeletedEqualTo(false);
        long l = orderMapper.countByExample(orderExample);
        return (int)l;
    }
}
